package univ.bme.accounting.beans;

import java.util.List;

import javax.annotation.PostConstruct;
import javax.ejb.EJB;
import javax.ejb.EJBException;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.RequestScoped;
import javax.faces.model.SelectItem;

import univ.bme.accounting.dao.TransactionDAO;
import univ.bme.accounting.domain.Transaction;


import univ.bme.accounting.domain.User;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;

@Getter
@Setter
@NoArgsConstructor

@ManagedBean
@RequestScoped
public class TransactionMB extends MBUtils {

	@EJB
	private TransactionDAO  transactiondao;

	private Transaction transaction;
	private Transaction selectedTr;
	
	@ManagedProperty(value="#{param.id}")
	private int id;
	
	@ManagedProperty(value="#{param.userid}")
	private int userId;
	
	private boolean invalidID;
	private boolean invalidUserID;
	
	private SelectItem[] typeOptions;  
	private SelectItem[] categoryOptions;
	
	List<Transaction> allTransactions;
	List<Transaction> filteredTransactions;
	
	@ManagedProperty("#{userMB}")
	private UserMB userMB;
	
	@ManagedProperty("#{accountingSessionBean}")
	private AccountingSessionBean accountingSessionBean;
	
	

	@PostConstruct
	public void init(){
		invalidUserID = false;
		checkUserId();
		allTransactions = transactiondao.findTransactionsForUser((userId));
		if(id >= 0){
			transaction = transactiondao.find(id);
			
			//There is some result, admin can view everything, I can view only my data and public data
			if(	transaction == null ||
				accountingSessionBean.isAccessForbidden(transaction.getUser()))
			{
				invalidID = true;
			}
			else {
				invalidID = false;
			}
		}
	}
	
	private void checkUserId(){
		User chart_user;
		if(!(userId > 0)){
			chart_user =  accountingSessionBean.getUser();
			userId =chart_user.getId();
			
		}
		else{
			chart_user = accountingSessionBean.getUserdao().find(userId);
			if( chart_user == null || accountingSessionBean.isAccessForbidden(chart_user))
				invalidUserID = true;
			
		}
		userMB.setUser(chart_user);
	}

	public Transaction getTransaction() {
		if(transaction == null){
			transaction = new Transaction();
		}
		return transaction;
	}
	
	public List<Transaction> getAllTransactions(){
		 return allTransactions;
	}
	
	public void setAllTransactions(List<Transaction> transactions) {
        this.allTransactions = transactions;
    }

	public String deleteTransaction(){
		try {
			transactiondao.delete(transaction);
		} catch (EJBException e) {
			sendErrorMessageToUser(bundle.getString("operationFailed"));
			return null;
		}
		sendInfoMessageToUser(bundle.getString("operationCompleted"));
		return "listTransactions?faces-redirect=true";
	}
	
	public String createTransaction(){
		try {
			//Add the user from the session
			transaction.setUser(accountingSessionBean.getUser());
			transactiondao.save(transaction);
		} catch (EJBException e) {
			sendErrorMessageToUser(bundle.getString("allrequired"));
			return null;
		}		
		sendInfoMessageToUser(bundle.getString("operationCompleted"));
		return "listTransactions?faces-redirect=true";
	}
	
	public String goToChart(){
		return "transactionStatistics?userid="+userMB.getCurrentRequestUser().getId()+"&faces-redirect=true";
	}
	
	public String goToDetails(int id){
		System.out.println("Go to detials");
		return "transactionDetails?id="+id+"&faces-redirect=true";
	}
}